$(document).ready(function () {
	//Cache event click comment
	$("#newsComment .openComment").bind("click", eventClickComment);
	
	function eventClickComment() {
		if ($(this).hasClass("open")) {
			$(this).removeClass("open");
			$(this).addClass("close");
			$(this).html("Open Comments");
			
			$("#newsComment .newsCommentForm ").fadeOut(500);
			$("#newsComment .commentListing").fadeOut(500);
			
		} else {
			$(this).addClass("open");
			$(this).removeClass("close");
			$(this).html("Close Comments");
			var form_data = {newsID:$(".right_inner ul.newsListing li.active a").attr("id")};
			$.ajax({
				type: "POST",
				url: "ajax_news_comment",
				data: form_data,
				dataType: "html",
				success: function(data)
				{
					$("#newsComment .commentListing").html(data);
					$("#newsComment .newsCommentForm ").fadeIn(1000);
					$("#newsComment .commentListing").fadeIn(1000);
					
				}
			});
		}
	}
	
	var txt_comment = $(".newsCommentForm .commentArea textarea").val();
	//Set event focus
	$(".newsCommentForm .commentArea textarea").focus(function () {
		if ($(this).val() == txt_comment) {
			$(this).val("");
		}
	});
	$(".newsCommentForm .commentArea textarea").focusout(function () {
		if ($(this).val() == "") {
			$(this).val(txt_comment);
		}
	});
	
	//Submit form
	$(".newsCommentForm form button").click(function () {
		var txt = $(".newsCommentForm .commentArea textarea").val();
		if (txt == txt_comment) {
			alert("Please enter content comment");
		} else {
			var form_data = {
					newsID:$(".right_inner ul.newsListing li.active a").attr("id"),
					comment: txt
			};
			$.ajax({
				type: "POST",
				url: "ajax_post_comment",
				data: form_data,
				dataType: "html",
				success: function(data)
				{
					$(".commentListing > ul").prepend(data);
					$(".commentListing > ul > li").eq(0).hide().show("blind", {percent: 100}, 1000);
					var count_comment = $(".commentListing > ul > li").length + 1;
					$(".commentListing > h3").html(count_comment + " comments");
					$(".newsInfo .right").html(count_comment + " Comments");
				}
			});
		}
		return false;
	});
});